<template>
  <div class="publish-container">
    <el-card>
      <my-bread>发布文章</my-bread>
      <el-form :model="form" :rules="rules" ref="form" label-width="100px" class="demo-form">
        <el-form-item label="标题" prop="title">
            <el-input v-model="form.title"></el-input>
        </el-form-item>
        <!-- <el-form-item label="内容" prop="content">
            <el-input type="textarea" v-model="form.content"></el-input>
        </el-form-item> -->
        <el-form-item label="内容" prop="content">
          <quill-editor v-model="form.content"
            :options="editorOption">
          </quill-editor>
        </el-form-item>
        <el-form-item label="封面">
            <el-radio-group v-model="form.coverType" @change="hReSetImageList">
              <el-radio :label="1">单图</el-radio>
              <el-radio :label="3">三图</el-radio>
              <el-radio :label="0">无图</el-radio>
              <el-radio :label="-1">自动</el-radio>
            </el-radio-group>
        </el-form-item>
        <el-form-item label="">
          <el-row>
            <el-col :span="6"
              v-for="(item,idx) in form.coverType"
              :key="idx">{{idx}}
              <!-- v-model="form.coverImages[idx]" 加下标[idx]，才能使coverImages是个 数组存放多张图片 -->
              <Cover v-model="form.coverImages[idx]"></Cover>
            </el-col>
          </el-row>
        </el-form-item>
        <Channel v-model="form.channelId"></Channel>
        <el-form-item>
            <el-button type="primary" @click="submitForm(false)">发布</el-button>
            <el-button @click="submitForm(true)">存入草稿</el-button>
        </el-form-item>
        </el-form>
    </el-card>
  </div>
</template>

<script>
import 'quill/dist/quill.core.css'
import 'quill/dist/quill.snow.css'
import 'quill/dist/quill.bubble.css'
import { quillEditor } from 'vue-quill-editor'
export default {
  name: 'Add',
  components: {
    quillEditor
  },
  data () {
    return {
      editorOption: {
        // 对富文本编辑器的配置
        // 占位配置
        placeholder: '',
        modules: {
          // 配置工具栏
          toolbar: [
            ['bold', 'italic', 'underline', 'strike'],
            ['blockquote', 'code-block'],
            [{ header: 1 }, { header: 2 }],
            [{ list: 'ordered' }, { list: 'bullet' }],
            [{ indent: '-1' }, { indent: '+1' }],
            ['image']
          ]
        }
      },
      form: {
        title: '',
        // 内容
        content: '这里是内容',
        // 封面
        coverType: 0, // 封面类型 -1:自动，0-无图，1-1张，3-3张
        // 保存对应封面图的地址
        coverImages: [],
        // 文章所属频道id
        channelId: ''
        // 是否为草稿
        // isdraft: false
      },
      // 校验规则
      rules: {
        title: [
          { required: true, message: '请输入标题', trigger: 'blur' },
          { min: 5, max: 30, message: '长度在 5 到 30 个字符', trigger: 'blur' }
        ],
        content: [
          { required: true, message: '请输入内容', trigger: 'blur' }
        ],
        // cover: [
        //   { required: true, message: '请选择封面', trigger: 'blur' }
        // ],
        channelId: [
          { required: true, message: '请选择频道', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    // 发布 / 存入草稿
    submitForm (isdraft) {
      // 1.整体表单校验
      this.$refs.form.validate((valid) => {
        if (valid) {
          this.$message.success('校验成功')
          // 校验成功，发起请求
          this.publish(isdraft)
        } else {
          this.$message.error('校验失败')
        }
      })
    },
    async publish (isdraft) {
      // 2.调接口，发请求
      try {
        const result = await this.$http({
          method: 'post',
          url: '/mp/v1_0/articles?draft=' + isdraft,
          data: {
            title: this.form.title,
            content: this.form.content,
            cover: {
              type: this.form.coverType,
              images: this.form.coverImages
            },
            channel_id: this.form.channelId
          }
        })
        console.log(result)
        // 清空表单
        this.form.title = ''
      } catch (error) {
        console.log(error)
      }
      // 3.更新视图
      // 4.跳转到内容管理
    },
    // 切换封面类型时，重新选择封面
    hReSetImageList () {
      if (this.form.coverType === 3) {
      } else if (this.form.coverType === 1) {
        // 如果封面类型是三图，则切换到单图时删除后两张图片，在coverImages中保留第一张即可
        // 方式一
        // this.form.coverImages.splice(1, 2)
        // 方式二
        this.form.coverImages = [this.form.coverImages[0]]
        console.log(this.form.coverImages)
      }
    }
  }
}
</script>

<style lang="less" scoped>
.el-form {
  padding-top: 25px;
}
// 在父组件中去设置子组件的样式，加上/deep/ 实现穿透功能
.publish-container /deep/ .ql-editor{
  height: 300px;
}
.publish-container /deep/ .ql-toolbar.ql-snow{
  padding: 0 8px;
}
</style>
